﻿using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;

namespace Winnow.SharepointDesigner.Layouts.Winnow.SharepointDesigner
{
    public partial class List : LayoutsPageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                Guid webId = new Guid(Request.QueryString["WebId"]);
                Guid listId = new Guid(Request.QueryString["ListId"]);


                using (SPWeb web = SPContext.Current.Site.RootWeb.Site.OpenWeb(webId))
                {
                    web.Lists.ListsForCurrentUser = true;
                    List<SPList> lists = web.Lists.Cast<SPList>().ToList();

                    var list = web.Lists.GetList(listId, false);
                    lblListName.Text = list.Title;
                    lblDefaultView.Text = list.DefaultViewUrl;
                    lblDefaultView.NavigateUrl = list.DefaultViewUrl;
                    hlkAddNewItem.Text = list.DefaultNewFormUrl;
                    hlkAddNewItem.NavigateUrl = list.DefaultNewFormUrl;
                    lblEdit.Text = list.DefaultEditFormUrl;
                    lblDefaultMobileView.Text = list.MobileDefaultViewUrl;
                    lblDefaultMobileView.NavigateUrl = list.MobileDefaultViewUrl;
                    hlkMobileAddNewItem.Text = list.MobileDefaultNewFormUrl;
                    hlkMobileAddNewItem.NavigateUrl = list.MobileDefaultNewFormUrl;
                    lblMobileEdit.Text = list.MobileDefaultEditFormUrl;
                    img.ImageUrl = list.ImageUrl;
                    lblAuther.Text = list.Author.Name;
                    lblID.Text = list.ID.ToString();
                    lblBaseType.Text = list.BaseType.ToString();
                    lblCreatedOn.Text = list.Created.ToShortDateString();
                    lblDescription.Text = list.Description;
                    lblItemCount.Text = list.ItemCount.ToString();
                    // grdEvents.DataSource = list.EventReceivers;
                    // grdEvents.DataBind();
                    grdViews.DataSource = list.Views;
                    grdViews.AutoGenerateColumns = false;
                    grdViews.DataBind();
                    grdColumns.DataSource = list.Fields;
                    grdColumns.AutoGenerateColumns = false;
                    grdColumns.DataBind();

                    //XDocument XDoc = new XDocument();

                    //foreach (var item in list.Forms.Cast<SPForm>())
                    //{
                    //    var t = item.SchemaXml;
                    //     XDoc.Element().Add(XDoc.crea
                    //}

                    grdForms.DataSource = list.Forms;
                    grdForms.DataBind();
                }

            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
        }
    }
}
